<?php
/**
 * User: ppjun378
 * Data: 2019/01/
 * Time: 18:00
 */
namespace App\Service;

use App\Model\EducationAndTrainings;

class EducationAndTrainingsService
{
    /**
     * 获取数据
     * @param $request
     * @return mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function dataList($resume_id)
    {
        $fields = [
            'id as eid', // 家庭关系id
            'edu_start_stop_time', //起止时间
            'school_name', //学校名称
            'professional', //专业
            'academic_degree', //所获学历、学位
        ];

        $data = EducationAndTrainings::select($fields)
            ->where('resume_id', $resume_id)
            ->orderBy('id', 'asc')
            ->get();

        $data = empty($data) ? array() : $data->toArray();

        if (!empty($data)) {
            foreach ($data as $key => $val) {
                $time = explode('/', $val['edu_start_stop_time']);
                $data[$key]['edu_start_time'] = $time[0];
                $data[$key]['edu_stop_time'] = $time[0];
            }
        }

        return $data;

    }

    /**
     * 添加/更新
     * @param $request
     * @return mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function store($request, $id)
    {
        $education_and_trainings = $request->input('education_and_trainings', '');
        // $education_and_trainings = '[{"eid": 1,"edu_start_stop_time": "20170901-20180627","school_name": "房地讲小学","professional": "挖掘机","academic_degree": "小学士"}]';
        if (empty($education_and_trainings)) {
            return false;
        }

        $education_and_trainings = json_decode($education_and_trainings, true);

        $map = [];
        foreach ($education_and_trainings as $key => $val) {
            $map['edu_start_stop_time'] = $val['edu_start_stop_time'];
            $map['school_name'] = $val['school_name'];
            $map['professional'] = $val['professional'];
            $map['academic_degree'] = $val['academic_degree'];

            if (!empty($val['eid'])) {
                $data_exist = EducationAndTrainings::where('id', $val['eid'])->first();
                if ($data_exist) {
                    // 更新信息
                    $result = EducationAndTrainings::where('id', $val['eid'])->update($map);
                } else {
                    $map['resume_id'] = $id;
                    // 添加信息
                    $result = EducationAndTrainings::create($map);
                }
            } else {
                $map['resume_id'] = $id;
                // 添加信息
                $result = EducationAndTrainings::create($map);

            }

        }

    }

    /**
     * 删除
     * @param int $id
     * @return bool\Intervention
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function destory($request)
    {
        $id = $request->input('id', '');
        $res = EducationAndTrainings::destory($id);
        if ($res > 0) {
            return $res;
        } else {
            return false;
        }
    }

    /**
     * 恢复删除
     * @param $int $id
     * @return mixed
     * @author ppjun378 <ppjun378@foxmail.com>
     */
    public static function restore($request)
    {
        $id = $request->input('id', '');
        $res = EducationAndTrainings::restore();
        return $res;
    }

}
